"use strict";
cc._RF.push(module, 'afb09sbSepOVZFL0TzhZYrc', 'GameDrop');
// scripts/GameDrop.ts

Object.defineProperty(exports, "__esModule", { value: true });
var Config_1 = require("./Config");
var Model_1 = require("./Model");
var MetaballMgr_1 = require("./MetaballMgr");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GameDrop = /** @class */ (function (_super) {
    __extends(GameDrop, _super);
    function GameDrop() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.world = null;
        _this.Ball = null;
        _this.Cup = null;
        _this.Bricks = [];
        _this.drawMgr = null;
        _this.TopUI = null;
        _this.BottomUI = null;
        _this.countDownNode = null;
        _this.countDownTime = 0;
        _this.ground = null;
        _this.SmokeEff = null;
        _this.bricks = [];
        _this.dynamicObjs = [];
        _this.isChecking = false;
        _this.lock = false;
        _this.waters = [];
        return _this;
    }
    GameDrop.prototype.onLoad = function () {
        Config_1.default.init();
        //开启物理效果
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().gravity = new cc.Vec2(0, -500);
        cc.director.getActionManager()['enabledAccumulator'] = true;
        cc.director.getActionManager()['FIXED_TIME_STEP'] = 1 / 30;
        cc.director.getPhysicsManager()['VELOCITY_ITERATIONS'] = 8;
        // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_jointBit | cc.PhysicsManager.DrawBits.e_shapeBit;
        this.createWorld();
        var top = cc.instantiate(this.TopUI);
        this.node.addChild(top);
        this.drawMgr.getComponent(MetaballMgr_1.default).waters = this.waters;
        this.node.getChildByName('gameTip').active = Model_1.default.game.selectedLevel == 1;
        console.log("@@@@@@@@@@@@@@@@@111");
    };
    GameDrop.prototype.start = function () {
    };
    GameDrop.prototype.createWorld = function () {
        var _this = this;
        //createCup
        var level = Model_1.default.game.selectedLevel;
        var data = Config_1.default.brickLevels[level];
        var cupPos = data[0];
        var cup = cc.instantiate(this.Cup);
        cup.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
        cup.x = cupPos[0];
        cup.y = cupPos[1];
        this.node.addChild(cup);
        cup.getComponent(cc.RigidBody).onBeginContact = function (contact, selfCollider, otherCollider) {
            // this.onBeginContact(contact, selfCollider, otherCollider)
        };
        var cupData = Config_1.default.cupDatas[1];
        for (var i = 0; i < cupData.polygonColliderList.length; i++) {
            var collider = cup.addComponent(cc.PhysicsPolygonCollider);
            collider.points = cupData.polygonColliderList[i];
            collider.density = 2;
            collider.apply();
        }
        this.cup = cup;
        //添加水滴
        for (i = 0; i < 24; i++) {
            var ball = cc.instantiate(this.Ball);
            ball.x = cup.x + Math.random() * 8 - 16;
            ball.y = cup.y + Math.random() * 50;
            this.world.addChild(ball, -1);
            this.waters.push(ball);
        }
        //bricks
        for (var i = 1; i < data.length; i++) {
            var brickType = data[i][0];
            var brick = cc.instantiate(this.Bricks[brickType - 1]);
            brick.x = data[i][1];
            brick.y = data[i][2];
            if (brickType == 1) //矩形
             {
                brick.setContentSize(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsBoxCollider).size = cc.size(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsBoxCollider).apply();
            }
            brick.angle = -data[i][5];
            this.node.addChild(brick);
            brick.on(cc.Node.EventType.TOUCH_START, this.onBrickTouch, this);
            this.bricks.push(brick);
            if (brick.getComponent(cc.RigidBody) != null) {
                if (brick.getComponent(cc.RigidBody).type == cc.RigidBodyType.Dynamic) {
                    this.dynamicObjs.push(brick);
                }
                brick.getComponent(cc.RigidBody).type = cc.RigidBodyType.Static;
            }
            brick.getComponent(cc.RigidBody).enabledContactListener = true;
            brick.getComponent(cc.RigidBody).onBeginContact = function (contact, selfCollider, otherCollider) {
                _this.onBeginContact(contact, selfCollider, otherCollider);
            };
        }
        this.ground.getComponent(cc.RigidBody).onBeginContact = function (contact, selfCollider, otherCollider) {
            _this.onBeginContact(contact, selfCollider, otherCollider);
        };
    };
    GameDrop.prototype.onBrickTouch = function (e) {
        if (this.lock)
            return;
        var brick = e.target;
        for (var i = 0; i < this.bricks.length; i++) {
            if (this.bricks[i] == brick) {
                this.bricks.splice(i, 1);
                break;
            }
        }
        for (var _i = 0, _a = this.dynamicObjs; _i < _a.length; _i++) {
            var obj = _a[_i];
            obj.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
        }
        this.dynamicObjs = [];
        brick.destroy();
    };
    GameDrop.prototype.update = function (dt) {
        this.drawMgr.getComponent(MetaballMgr_1.default).draw();
        if (this.lock)
            return;
        // this.checkGame()
    };
    GameDrop.prototype.checkGame = function () {
        if (!this.isChecking) {
            this.isChecking = true;
            console.log('检测');
            this.countDownTime = 3;
            this.countDownNode.active = true;
            this.schedule(this.startCountDown, 1);
        }
    };
    GameDrop.prototype.startCountDown = function () {
        this.countDownNode.getChildByName('p0').active = false;
        this.countDownNode.getChildByName('p1').active = false;
        this.countDownNode.getChildByName('p2').active = false;
        this.countDownNode.getChildByName('p3').active = false;
        this.countDownTime--;
        this.countDownNode.getChildByName('p' + this.countDownTime).active = true;
        if (this.countDownTime <= 0) {
            this.countDownNode.getChildByName('p0').active = false;
            this.unschedule(this.startCountDown);
            //this.schedule(this.checkBallSleep, 0.016)
            Model_1.default.game.dropLevel++;
            Model_1.default.game.win = true;
            this.showFace(2);
            this.scheduleOnce(this.delayShowEnd.bind(this), 1);
        }
    };
    GameDrop.prototype.delayShowEnd = function () {
        cc.director.loadScene('gameEnd');
    };
    GameDrop.prototype.showFace = function (type) {
        this.cup.getChildByName('facenormal').active = false;
        this.cup.getChildByName('facewin').active = false;
        this.cup.getChildByName('facelose').active = false;
        if (type == 2) {
            this.cup.getChildByName('facewin').active = true;
        }
        else if (type == 1) {
            this.cup.getChildByName('facelose').active = true;
        }
        else if (type == 0) {
            this.cup.getChildByName('facenormal').active = true;
        }
    };
    GameDrop.prototype.onBeginContact = function (contact, selfCollider, otherCollider) {
        //  console.log("onBeginContact:", otherCollider.node.name, " ", selfCollider.node.name);
        // console.log("onBeginContact", selfCollider.node.group, otherCollider.node.group);
        // console.log("onBeginContact", selfCollider.node.groupIndex, otherCollider.node.groupIndex);
        if (this.lock)
            return;
        if (selfCollider.node.name == 'ground' && otherCollider.node.name == 'Ball') {
            Model_1.default.game.win = false;
            this.showFace(1);
            this.scheduleOnce(this.delayShowEnd.bind(this), 1);
            this.lock = true;
            for (var i = 0; i < this.waters.length; i++) {
                if (this.waters[i] == otherCollider.node) {
                    this.waters.splice(i, 1);
                    i--;
                    this.createSmokeEff(otherCollider.node.x, otherCollider.node.y);
                    otherCollider.node.destroy();
                    break;
                }
            }
        }
        if (selfCollider.node.name == 'ground' && otherCollider.node.name == 'Cup2') {
            this.lock = true;
            this.checkGame();
        }
        if (selfCollider.node.name.indexOf('brick') != -1 && otherCollider.node.name == 'Ball') {
            Model_1.default.game.win = false;
            this.showFace(1);
            this.scheduleOnce(this.delayShowEnd.bind(this), 1);
            this.lock = true;
        }
    };
    GameDrop.prototype.createSmokeEff = function (x, y) {
        var eff = cc.instantiate(this.SmokeEff);
        eff.x = x;
        eff.y = y;
        this.node.addChild(eff);
    };
    __decorate([
        property(cc.Node)
    ], GameDrop.prototype, "world", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrop.prototype, "Ball", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrop.prototype, "Cup", void 0);
    __decorate([
        property([cc.Prefab])
    ], GameDrop.prototype, "Bricks", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrop.prototype, "drawMgr", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrop.prototype, "TopUI", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrop.prototype, "BottomUI", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrop.prototype, "countDownNode", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrop.prototype, "ground", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrop.prototype, "SmokeEff", void 0);
    GameDrop = __decorate([
        ccclass
    ], GameDrop);
    return GameDrop;
}(cc.Component));
exports.default = GameDrop;

cc._RF.pop();